<?php

namespace App\Http\Requests\Admin\CollTwoOption;
use App\Http\Requests\Request;
use App\Models\Common\CollTwoOption;
use Validator;
class UpdateTwoRequest extends Request{
    public function authorize(){
        return true;
    }
    public function rules(){
        $input = $this->all();
        //添加编码排序判断
        Validator::extend('check_two_opt_sort', function($attribute, $value, $parameters) {
            if($value!=0){
                $CollTwoOptionSort = CollTwoOption::where('sign', $this->sign)->where('pid', $this->pid)
                    ->where('id','!=', $this->id)->pluck('sort')->toArray();
                if(in_array($value,$CollTwoOptionSort)){
                    return false;
                }else{
                    return true;
                }
            }else{
                return true;
            }
        });
        return [
            'name'=>'required',
            'cid'=>'required|numeric|update_two_sign_match:'.$input['pid'].','.$input['id'],
            'sort'=>'check_two_opt_sort'
        ];
    }
    public function messages(){
        return [
            'name.required'=>'分类选项名称必须填写',
            'cid.update_two_sign_match'=>'当前分类选项编码已经存在',
            'cid.required'=>'分类选项编码必须填写',
            'cid.numeric'=>'分类选项编码必须是一个数字',
            'sort.check_two_opt_sort'=>'当前分类选项排序已经存在'
        ];
    }
}
